package org.Panel;

import org.Component.CommentBar;
import org.Tools.OnLineOP;
import org.implement.Panel.PanelSkip;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CommentPanel extends JPanel {

    //评论条
    CommentBar commentBar;

    //发表按钮
    JButton sendButton = new JButton("发表");

    //文本框
    JTextArea commentTextArea = new JTextArea();
    public String hint = "hint";//提示文本
    JLabel hintLabel = new JLabel();    //隐藏文本

    //输入框的滚动
    JScrollPane scrollPane1 = new JScrollPane(commentTextArea);

    //收缩按钮
    JButton unfold = new JButton("我也说一句");

    //评论发表区
    JPanel CommentPanel = new JPanel();

    // 创建一个简单的线边框，颜色为黑色，粗细为1像素
    Border border = BorderFactory.createLineBorder(Color.BLACK, 1);

    //记录评论的长度
    int CHeight = 0;

    //记录本界面的大小
    int Width = 620;
    int Height = 160;

    //回复对象标识
    public int replyJudge = 0;
    //回复对象的用户名
    public String replyUser = "";
    //母控件跳转接口
    PanelSkip panelSkip;

    public CommentPanel(PanelSkip panelSkip) {
        this.panelSkip = panelSkip;
        setBounds(0,0,820,160);
        setLayout(null);
        setBorder(border);
        setBackground(Color.LIGHT_GRAY);

        //发表评论的折叠区
        CommentPanel.setBounds(5,50,810,100);
        CommentPanel.setLayout(null);

        //文本区,将文本区改为分行
        commentTextArea.setLineWrap(true);
        commentTextArea.setWrapStyleWord(true);
        //文本框的提示文本设置
        hintLabel.setBounds(15,10,200,20);
        hintLabel.setForeground(Color.GRAY);
        hintLabel.setText(hint);
        commentTextArea.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(javax.swing.event.DocumentEvent e) {
                updateHintVisibility();
            }
            @Override
            public void removeUpdate(javax.swing.event.DocumentEvent e) {
                updateHintVisibility();
            }
            @Override
            public void changedUpdate(javax.swing.event.DocumentEvent e) {
                updateHintVisibility();
            }
        });
        //文本区的滚动界面
        scrollPane1.setBounds(10,10,780,40);

        //发表按钮
        sendButton.setBounds(730,60,80,40);
        sendButton.addMouseListener(new MouseAdapter() {
            int f=1;
            @Override
            public void mousePressed(MouseEvent e) {}

            @Override
            public void mouseReleased(MouseEvent e) {
                if(f==1) {
                    sendComment();
                    replyJudge=0;
                    hint="";
                    updateHintVisibility();
//                    panelSkip.skip(5);
                }
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                f=1;
            }

            @Override
            public void mouseExited(MouseEvent e) {
                f=0;
            }
        });

        //收缩按钮
        unfold.setBounds(730,10,80,40);
        unfold.addMouseListener(new MouseAdapter() {
            int f=1;
            @Override
            public void mousePressed(MouseEvent e) {}

            @Override
            public void mouseReleased(MouseEvent e) {
                if(f==1) {
                    if(CommentPanel.getHeight()<=0) {
                        Unfold();
                    }else {
                        Fold();
                        replyJudge = 0;
                        replyUser = "";
                        hint = "";
                        updateHintVisibility();
                    }
                }
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                f=1;
            }

            @Override
            public void mouseExited(MouseEvent e) {
                f=0;
            }
        });

        //将发表评论相关组件添加到评论界面
        CommentPanel.add(hintLabel);
        CommentPanel.add(scrollPane1);
        CommentPanel.add(sendButton);


        //添加到界面
        add(unfold);
        add(CommentPanel);

        //默认评论按钮收缩
        Fold();

    }

    //文本框的提示文本显示
    public void updateHintVisibility() {
        if (commentTextArea.getText().length() == 0) {
            hintLabel.setForeground(Color.GRAY);
            hintLabel.setText(hint);
        } else {
            hintLabel.setForeground(Color.BLACK);
            hintLabel.setText("");
        }
    }

    //发表评论
    private void sendComment() {
        String str = commentTextArea.getText();
        //获取当前时间
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDate = sdf.format(now);
        CommentBar com = new CommentBar(MainPanel.userName,str,formattedDate,this);
        //服务器传输
        String msg = "reply-{\"postId\":"+MainPanel.post.getPostId()+",\"content\":\""+str+"\",\"userId\":"+MainPanel.userId+",\"createdAt\":"+now.getTime()+"}-"+MainPanel.userName;
        System.out.println(msg);
        OnLineOP onLineOP = new OnLineOP();
        String jsonmsg;
        try {
            jsonmsg = onLineOP.sendone(msg);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println(jsonmsg);

//        if(replyJudge==1) {   //指向某个用户进行回复
//            com = new CommentBar("svc",replyUser,str,"1212-22-22 17:12",this);
//        }
        addCommentBar(com);
        commentTextArea.setText("");
    }

    //折叠评论发表框
    public void Fold() {
        CommentPanel.setSize(0,0);
        setSize(getWidth(),getHeight()-100);
    }

    //打开评论发表框
    public void Unfold() {
        CommentPanel.setSize(810,100);
        setSize(getWidth(),getHeight()+100);
    }

    //添加聊天条
    public void addCommentBar(CommentBar commentBar) {
        //添加聊天条
        add(commentBar);
        commentBar.setLocation(50,CHeight+5);
        //更新聊天框的长度
        CHeight += commentBar.getHeight()+5;
        //更新面板的大小和各组件的位置
        if(CommentPanel.getHeight()<=0) {
            setSize(getWidth(),CHeight+60);
        }else {
            setSize(getWidth(),CHeight+160);
        }
        Width = getWidth();
        Height = getHeight();
        unfold.setLocation(unfold.getX(),10+CHeight);
        CommentPanel.setLocation(CommentPanel.getX(),50+CHeight);

    }

}
